﻿





using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.Andons.Dtos;
using KDS.Mes.Authorization;

#region 代码生成器相关信息_ABP Code Generator Info
//你好，我是ABP代码生成器的作者,欢迎您使用该工具，目前接受付费定制该工具，有需要的可以联系我
//我的邮箱:werltm@hotmail.com
// 官方网站:"http://www.yoyocms.com"
// 交流QQ群：104390185  
//微信公众号：角落的白板报
// 演示地址:"vue版本：http://vue.yoyocms.com angularJs版本:ng1.yoyocms.com"
//博客地址：http://www.cnblogs.com/wer-ltm/
//代码生成器帮助文档：http://www.cnblogs.com/wer-ltm/p/5777190.html
// <Author-作者>梁桐铭 ,微软MVP</Author-作者>
// Copyright © YoYoCms@China.2017-11-22T21:45:20. All Rights Reserved.
//<生成时间>2017-11-22T21:45:20</生成时间>
#endregion


namespace KDS.Mes.Mes.Andons
{
    /// <summary>
    /// andenf信息服务实现
    /// </summary>
   [AbpAuthorize(AppPermissions.Pages_ANDON)]


    public class ANDONAppService : MesAppServiceBase, IANDONAppService
    {
        private readonly IRepository<ANDON,int> _aNDONRepository;



        private readonly ANDONManage _aNDONManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public ANDONAppService( IRepository<ANDON,int> aNDONRepository	,
ANDONManage aNDONManage
	
  )
        {
            _aNDONRepository = aNDONRepository;
			 _aNDONManage = aNDONManage;
			 
        }


		  #region 实体的自定义扩展方法
        private IQueryable<ANDON> _aNDONRepositoryAsNoTrack => _aNDONRepository.GetAll().AsNoTracking();


        #endregion

         
        #region andenf信息管理
        public PagedResultDto<ANDONListDto> GetPagedANDONs(GetANDONInput input)
        {
            ////初步过滤
            var query =  _aNDONRepository.GetAll();
            //排序
            query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CreationTime);
            //获取总数
            var tasksCount = query.Count();
            //默认的分页方式
            //var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            //ABP提供了扩展方法PageBy分页方式
            var taskList = query.PageBy(input).ToList();

            return new PagedResultDto<ANDONListDto>(
             tasksCount,
             taskList.Select(item =>
             {
                 var dto = item.MapTo<ANDONListDto>();
                 return dto;
             }).ToList());




            ////初步过滤
            //var query = _partRepository.GetAll();
            ////排序
            //query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CreationTime);
            ////获取总数
            //var tasksCount = query.Count();
            ////默认的分页方式
            ////var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            ////ABP提供了扩展方法PageBy分页方式
            //var taskList = query.PageBy(input).ToList();
            //return new PagedResultDto<PartListDto>(tasksCount, taskList.MapTo<List<PartListDto>>());
        }
        /// <summary>
        /// 根据查询条件获取andenf信息分页列表
        /// </summary>
        public async Task<PagedResultDto<ANDONListDto>> GetPagedANDONsAsync(GetANDONInput input)
{
			
    var query = _aNDONRepositoryAsNoTrack;
    //TODO:根据传入的参数添加过滤条件

    var aNDONCount = await query.CountAsync();

    var aNDONs = await query
   // .OrderBy(input.Sorting)
    .PageBy(input)
    .ToListAsync();

    var aNDONListDtos = aNDONs.MapTo<List<ANDONListDto>>();
    return new PagedResultDto<ANDONListDto>(
    aNDONCount,
    aNDONListDtos
    );
    }

        /// <summary>
    /// 通过Id获取andenf信息信息进行编辑或修改 
    /// </summary>
    public async Task<GetANDONForEditOutput> GetANDONForEditAsync(NullableIdDto<int> input)
{
    var output=new GetANDONForEditOutput();

    ANDONEditDto aNDONEditDto;

    if(input.Id.HasValue)
	{
    var entity=await _aNDONRepository.GetAsync(input.Id.Value);
    aNDONEditDto=entity.MapTo<ANDONEditDto>();
    }
	else 
	{
	aNDONEditDto=new ANDONEditDto();	
	}

	output.ANDON=aNDONEditDto;
	return output;
    }


    /// <summary>
    /// 通过指定id获取andenf信息ListDto信息
    /// </summary>
    public async Task<ANDONListDto> GetANDONByIdAsync(EntityDto<int> input)
{
    var entity = await _aNDONRepository.GetAsync(input.Id);

    return entity.MapTo<ANDONListDto>();
    }







    /// <summary>
    /// 新增或更改andenf信息
    /// </summary>
    public async Task CreateOrUpdateANDONAsync(CreateOrUpdateANDONInput input)
{
    if (input.ANDONEditDto.Id.HasValue)
{
    await UpdateANDONAsync(input.ANDONEditDto);
    }
    else
{
    await CreateANDONAsync(input.ANDONEditDto);
    }
    }

    /// <summary>
    /// 新增andenf信息
    /// </summary>
   [AbpAuthorize(AppPermissions.Pages_ANDON_CreateANDON)]	 
         public virtual async Task<ANDONEditDto> CreateANDONAsync(ANDONEditDto input)
{
    //TODO:新增前的逻辑判断，是否允许新增

	var entity = input.MapTo<ANDON>();
	
    entity = await _aNDONRepository.InsertAsync(entity);
    return entity.MapTo<ANDONEditDto>();
    }

    /// <summary>
    /// 编辑andenf信息
    /// </summary>
	      [AbpAuthorize(AppPermissions.Pages_ANDON_EditANDON)]
         public virtual async Task UpdateANDONAsync(ANDONEditDto input)
{
    //TODO:更新前的逻辑判断，是否允许更新

    var entity = await _aNDONRepository.GetAsync(input.Id.Value);
	input.MapTo(entity);

    await _aNDONRepository.UpdateAsync(entity);
    }

    /// <summary>
    /// 删除andenf信息
    /// </summary>
	    [AbpAuthorize(AppPermissions.Pages_ANDON_DeleteANDON)]
         public async Task DeleteANDONAsync(EntityDto<int> input)
{
    //TODO:删除前的逻辑判断，是否允许删除
    await _aNDONRepository.DeleteAsync(input.Id);
    }

        /// <summary>
    /// 批量删除andenf信息
    /// </summary>
	    [AbpAuthorize(AppPermissions.Pages_ANDON_DeleteANDON)]
         public async Task BatchDeleteANDONAsync(List<int> input)
{
    //TODO:批量删除前的逻辑判断，是否允许删除
    await _aNDONRepository.DeleteAsync(s=>input.Contains(s.Id));
    }

            #endregion
  









    }
    }
